using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.WebControls;
// DNN
using DotNetNuke;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Entities.Users;
// Codex
using CodexOmegaClassLibrary.Utils;
using CodexOmegaClassLibrary.Constants;
using System.Diagnostics;

namespace CodexOmega.Modules.VideoLinker
{
    partial class ViewVideoLinker : PortalModuleBase
    {
        #region Private Members

        private int _typeID = -1;
        private int _categoryID = -1;
        private string _keyword = string.Empty;

        #endregion

        #region Public Properties

        public int TypeID
        {
            get
            {
                if (_typeID > 0)
                {
                    return _typeID;
                }
                else
                {
                    return -1;
                }
            }
            set { _typeID = value; }
        }

        public int CategoryID
        {
            get
            {
                if (_categoryID > 0)
                {
                    return _categoryID;
                }
                else
                {
                    return -1;
                }
            }
            set { _categoryID = value; }
        }

        public string Keyword
        {
            get { return _keyword; }
            set { _keyword = value; }
        }

        #endregion

        #region Event Handlers

        protected void Page_Load(System.Object sender, System.EventArgs e)
        {
            // bind data
            if (!Page.IsPostBack)
            {
                BindData();
            }
            else
            {
                // get typeID
                TypeID = Convert.ToInt32(cboFilterType.SelectedItem.Value);

                // get categoryID
                CategoryID = Convert.ToInt32(cboFilterCategory.SelectedItem.Value);

                // get keyword
                Keyword = txtKeyword.Text.Trim();
            }
        }

        protected void objVideosList_ObjectSelecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            // pass parameters to the ObjectDataSource
            e.InputParameters["typeID"] = TypeID;
            e.InputParameters["categoryID"] = CategoryID;
            e.InputParameters["keyword"] = Keyword;
        }

        protected void linkSearch_Click(System.Object sender, System.EventArgs e)
        {
            // rebind data
            gridVideos.DataBind();
        }

        protected void gridVideos_RowDataBound(Object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // set row color on mouse over/out
                if (e.Row.RowIndex % 2 == 1)
                {
                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#EEEEEE'");
                }
                else
                {
                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
                }
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#DDDDDD'");


                // set link 
                Label lblVideoID = (Label)e.Row.FindControl("lblVideoID");
                LinkButton linkTitle = (LinkButton)e.Row.FindControl("linkTitle");


                // change link on edit if user is admin
                if (IsCurrentUserAdmin())
                {
                    linkTitle.PostBackUrl = DotNetNuke.Common.Globals.NavigateURL(PortalSettings.ActiveTab.TabID, "Edit", "mid=" + ModuleId.ToString() + "&id=" + lblVideoID.Text);
                }
                else
                {
                    linkTitle.PostBackUrl = DotNetNuke.Common.Globals.NavigateURL(PortalSettings.ActiveTab.TabID, "VideoDetail", "mid=" + ModuleId.ToString() + "&id=" + lblVideoID.Text);
                }
            }
        }


        #endregion

        #region Methods

        private void BindData()
        {
            // localize Grid Headers
            Localization.LocalizeGridView(ref gridVideos, LocalResourceFile);

            // bind filter
            VideoLinkerController videoLinkerController = new VideoLinkerController();

            // type
            cboFilterType.DataSource = videoLinkerController.GetVideoTypesList();
            cboFilterType.DataTextField = "key";
            cboFilterType.DataValueField = "value";
            cboFilterType.DataBind();

            // category
            cboFilterCategory.DataSource = videoLinkerController.GetVideoCategoriesList();
            cboFilterCategory.DataTextField = "key";
            cboFilterCategory.DataValueField = "value";
            cboFilterCategory.DataBind();

            if (IsCurrentUserAdmin())
            {
                panelCommands.Visible = true;
            }
        }

        private bool IsCurrentUserAdmin()
        {
            bool isAdmin = false;

            // get current user
            UserInfo currentUser = UserController.GetCurrentUserInfo();

            // check user's role to show commands panel
            foreach (string role in currentUser.Roles)
            {
                if (role.ToLower() == ApplicationKeys.USER_ROLE_ARTICLE_MANAGER)
                {
                    isAdmin = true;
                    break;
                }
            }

            return isAdmin;
        }

        #endregion
    }
}

